package com.myslef.teaching_platfrom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.myslef.teaching_platfrom.domain.talk.Talk;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TalkMapper extends BaseMapper<Talk> {
    /**
     * 从数据库中获取评论列表
     *
     * @param classId
     * @return
     */
    @Select("select t.id, t.user_id, case when t.anonymity = 1 then '匿名用户' else u1.user_name end as user_name, " +
            "t.class_id, t2.id,case when t2.anonymity = 1 then '匿名用户' else (select user_name from user u where t2.user_id = u.id) end as before_name, " +
            "t.father_id, t.level, t.anonymity, t.content, " +
            "t.create_time ,t.img_url_list " +
            "from talk t join user u1 on t.user_id = u1.id left join talk t2 on t.before_talk_id = t2.id " +
            "where t.class_id = #{classId} group by t.create_time order by t.create_time desc")
    List<Talk> listCommentWithUserName(@Param("classId") String classId);

    @Select("SELECT (CASE t.anonymity WHEN 0 THEN u.pic_url ELSE \"uploads/portrait/anonymity.png\" END) " +
            " pic_url from user u join talk t on t.user_id = u.id where t.id = #{id} ")
    String getListPicUrl(Integer id);

    @Select("select id, content from talk where content like CONCAT('%', #{text}, '%') and class_id = #{classId} and level = 1")
    List<Talk> getSearchList(String text, String classId);

}